package cn.stylefeng.guns.modular.car.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.stylefeng.guns.modular.car.entity.Brand;
import cn.stylefeng.guns.modular.car.mapper.BrandMapper;
import cn.stylefeng.guns.modular.car.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ┌─┐       ┌─┐ + +
 * ┌──┘ ┴───────┘ ┴──┐++
 * │                 │
 * │       ───       │++ + + +
 * ███████───███████ │+
 * │                 │+
 * │       ─┴─       │
 * │                 │
 * └───┐         ┌───┘
 * │         │
 * │         │   + +
 * │         │
 * │         └──────────────┐
 * │                        │
 * │                        ├─┐
 * │                        ┌─┘
 * │                        │
 * └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 * │ ─┤ ─┤       │ ─┤ ─┤
 * └──┴──┘       └──┴──┘  + + + +
 * 神兽保佑
 * 代码无BUG!
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {


    @Override
    public List<Tree<Integer>> selectAll() {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的

        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parentId");
        // 最大递归深度
        List<Brand> brandList = this.baseMapper.selectList(null);
//        treeNodeConfig.setDeep(3);
        List<Tree<Integer>> treeList = TreeUtil.build(brandList, 0, treeNodeConfig, (brand, tree)  -> {
            tree.setId(brand.getId());
            tree.setParentId(brand.getParentId());
            tree.setName(brand.getName());


        });
        return treeList;
    }
}
